#pragma once

//string function
#include <string.h>

//malloc and free
#include <malloc.h>

//debug output
#include <iostream>
using std::cout;
using std::endl;

//logger of the memory allocation
#include "MemoryLogger.h"

//undef the existing new and delete. Maybe useless.
#undef delete
#undef new


inline void* operator new (size_t size, const char* file, int line)
{
	//do the allocation
	void* p = malloc(size);

	//log the allocation
	const char* name = strrchr(file, '\\');
	MEMLOG.NEW(p, size, name, line);

	//return the pointer
	return p;
}

inline void operator delete (void* p, const char* /*file*/, int /*line*/)
{
	MEMLOG.DEL(p);
	free(p);
}

//custom delete with classic parameters
inline void operator delete (void* p)
{	
	//log the delete
	MEMLOG.DEL(p);

	//deallocate
	free(p);
}

//custom array new
inline void* operator new[](size_t size, const char* file, int line)
{
	//allocate the memory
	void* p = malloc(size);

	//log the allocation
	const char* name = strrchr(file, '\\');
	MEMLOG.NEW(p, size, name, line);

	//return the pointer
	return p;
}

//custom array delete corresponding with the custum array new (needed for classes to avoid warning and crashes)
inline void operator delete[](void* p, const char* /*file*/, int /*line*/)
{
	//log the delete
	MEMLOG.DEL(p);

	//deallocate
	free(p);
}


//custom array delete with classic parameter
inline void operator delete[](void* p)
{
	//log the delete
	MEMLOG.DEL(p);

	//deallocate
	free(p);
}


//macro to call the custom new
#define __NEW__ new(__FILE__, __LINE__)
#define new __NEW__
